﻿using System.IO;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xom.Engine;
using XomSample;

namespace XOMSample.UnitTests
{
    /// <summary>
    ///This is a test class for BooksTest and is intended
    ///to contain all BooksTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BooksTest
    {
        private static XmlDocumentEngine Xom;
        
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Xom = new XmlDocumentEngine();
        }

        /// <summary>
        ///A test for Books Constructor
        ///</summary>
        [TestMethod()]
        public void BookConstructorTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleBookXmlFile();
            //act
            ICatalog catalog = Xom.StartMapping(sampleXml, typeof(Catalog)) as ICatalog;

            //assert
            Assert.IsNotNull(catalog);
            Assert.AreEqual(12, catalog.Books.Count);
        }

        /// <summary>
        /// Change existing properties of a book and check if the xml content has changed
        /// </summary>
        [TestMethod()]
        public void ChangeExistingBookPropertiesTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleBookXmlFile();
            ICatalog catalog = Xom.StartMapping(sampleXml, typeof(Catalog)) as ICatalog;

            //act
            catalog.Books[0].Genre = BookGenre.Horror;
            catalog.Books[1].Id = "bk102XXX";
            catalog.Books[2].Price = 10.99;

            //assert
            Assert.AreEqual("Horror", sampleXml.ChildNodes[1].FirstChild.ChildNodes[2].InnerText);
            Assert.AreEqual("bk102XXX", sampleXml.ChildNodes[1].ChildNodes[1].Attributes[0].InnerText);
            Assert.AreEqual("10.99", sampleXml.ChildNodes[1].ChildNodes[2].ChildNodes[3].InnerText);
        }

        /// <summary>
        /// Create a new book object
        /// </summary>
        [TestMethod()]
        public void CreateNewBookObjectTest()
        {
            //arrange
            XmlDocument sampleXml = GetSampleBookXmlFile();
            ICatalog catalog = Xom.StartMapping(sampleXml.ChildNodes[1], typeof(Catalog)) as ICatalog;

            //act
            IBook newBook = new Book();
            newBook.Id = "bk123";
            newBook.Price = 9.99;
            newBook.Title = "Book 123";
            catalog.Books.Add(newBook);

            //assert
            Assert.AreEqual(13, sampleXml.ChildNodes[1].ChildNodes.Count);
            Assert.AreEqual("<author /><title>Book 123</title><genre>Computer</genre><price>9.99</price><publish_date /><description />", sampleXml.ChildNodes[1].ChildNodes[12].InnerXml);
        }

        private XmlDocument GetSampleBookXmlFile()
        {
            XmlDocument xmlFile = new XmlDocument();
            xmlFile.Load(new MemoryStream(Properties.Resources.books));

            return xmlFile;
        }

    }
}
